package com.heba.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.heba.bean.article.ItemArticleObject;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.ItemArticleDetailActivity;
import com.heba.greenmulu.R;
import com.heba.utils.GlideUtil;
import com.heba.view.RoundImageView;

import java.util.List;

/**
 * ContentPagerAdapter
 * Created by jason on 2019/3/1.
 */

public class ContentPagerAdapter extends PagerAdapter {

    private Context context;
    private List<ItemArticleObject> list;
    private String columnName;

    public ContentPagerAdapter(Context context, List<ItemArticleObject> list, String columnName) {
        this.context = context;
        this.list = list;
        this.columnName = columnName;
    }
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        if (!list.isEmpty()) {
            final int park = position % list.size();
            View view = LayoutInflater.from(context).inflate(R.layout.item_home_viewpager_row, null);
            RoundImageView imageView = view.findViewById(R.id.img_photo);
            TextView txt_article = view.findViewById(R.id.txt_article);
            TextView txt_author = view.findViewById(R.id.txt_author);

            final ItemArticleObject object = list.get(park);
            view.setOnClickListener(v -> {
                Intent intent = new Intent(context, ItemArticleDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(CommonConfig.ARTICLE_ID, object.getArticleID());
                bundle.putString(CommonConfig.ARTICLE_TITLE, columnName);
                intent.putExtras(bundle);
                context.startActivity(intent);
            });
            GlideUtil.loadImageWithDefaultPlaceHolder(context, object.getImageUrl(), imageView);
            txt_article.setText(object.getTitle());
            txt_author.setText(object.getPublisher());
            view.setTag(position);
            container.addView(view, 0);
//            container.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return view;
        } else {
            return null;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}
